package com.jaryle.wenxin.util;

import com.alibaba.fastjson.JSONObject;
import com.jaryle.wenxin.bean.base.AccessToken;
import com.jaryle.wenxin.config.GlobalConstant;
import com.jaryle.wenxin.util.redis.RedisUtil;
import redis.clients.jedis.Jedis;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;


/**
 * 描述:
 * 微信工具类
 *
 * @author jaryle
 * @version V1.0
 * @create 2018-11-26
 */
public class WeiXinUtil {


    /**
     * 排序方法
     * @param token     Token
     * @param timestamp 时间戳
     * @param nonce     随机数
     * @return
     */
    public static String sort(String token, String timestamp, String nonce) {
        String[] strArray = {token, timestamp, nonce};
        Arrays.sort(strArray);
        StringBuilder sb = new StringBuilder();
        for (String str : strArray) {
            sb.append(str);
        }
        return sb.toString();
    }

    /**
     * 将字符串进行sha1加密
     *
     * @param str 需要加密的字符串
     * @return    加密后的内容
     */
    public static String sha1(String str) {
        if(str == null || str.equals("")){
            return null;
        }
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(str.getBytes());
            byte messageDigest[] = digest.digest();
            // 创建 16进制字符串
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     *
     * 获取AccessToken
     * @return 返回拿到的access_token及有效期
     * 并把acces_token存入redis中
     * @author jaryle
     */
    public static AccessToken getAccessToken(){
        AccessToken token = new AccessToken();
        Jedis jedis  = RedisUtil.getJedis();
        String result = HttpClientUtil.HttpGet(GlobalConstant.WEIXIN_ACCESS_TOKEN_URL);
        JSONObject jsonObject = JSONObject.parseObject(result);

        if(result !=null && ! "".equals(result)){
            token.setAccessToken(jsonObject.getString("access_token"));
            token.setExpiresIn(jsonObject.getString("expires_in"));
            jedis.set("access_token", jsonObject.getString("access_token"));
            jedis.expire("access_token", 60*60*2);
        }
        RedisUtil.returnResource(jedis);
        return token;
    }

    /**
     * 从redis中获取凭证
     * @return
     */
    public static String  getAccess_Token(){
        System.out.println("从缓存中读取");
        Jedis jedis  = RedisUtil.getJedis();
        String access_token = jedis.get("access_token");
        if(access_token==null || "".equals(access_token)){
            AccessToken token = getAccessToken();
            access_token = token.getAccessToken();
        }
        RedisUtil.returnResource(jedis);
        return access_token;
    }

}
